using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Microsoft.LearningComponents;
namespace ValidatePackage
{
public partial class ValidatePackage : Form
{
public ValidatePackage(string initialFileOrFolder)
{
InitializeComponent();
if (initialFileOrFolder != null)
{
if (Directory.Exists(initialFileOrFolder))
{
folderTextBox.Text = initialFileOrFolder;
PopulatePackageList(null);
}
else
if (File.Exists(initialFileOrFolder))
{
folderTextBox.Text = Path.GetDirectoryName(initialFileOrFolder);
PopulatePackageList(Path.GetFileName(initialFileOrFolder));
}
}
}
private void viewButton_Click(object sender, EventArgs e)
{
PopulatePackageList(null);
}
private void browseButton_Click(object sender, EventArgs e)
{
folderBrowserDialog.SelectedPath = folderTextBox.Text;
if (folderBrowserDialog.ShowDialog() == DialogResult.Cancel)
return;
folderTextBox.Text = folderBrowserDialog.SelectedPath;
PopulatePackageList(null);
}
void PopulatePackageList(string fileNameToSelect)
{
int selectedIndex = -1;
try
{
packageListBox.Items.Clear();
foreach (string filePath in Directory.GetFiles(folderTextBox.Text))
{
string extension = Path.GetExtension(filePath);
if (!String.Equals(extension, ".zip", StringComparison.OrdinalIgnoreCase) &&
!String.Equals(extension, ".lrm", StringComparison.OrdinalIgnoreCase) &&
!String.Equals(extension, ".ims", StringComparison.OrdinalIgnoreCase))
continue;
int index = packageListBox.Items.Add(new FileListItem(filePath));
if (String.Equals(fileNameToSelect, Path.GetFileName(filePath), StringComparison.OrdinalIgnoreCase))
selectedIndex = index;
}
}
catch (DirectoryNotFoundException)
{
folderTextBox.SelectAll();
MessageBox.Show("Invalid folder");
}
logTextBox.Clear();
packageListBox.SelectedIndex = selectedIndex;
}
private void packageListBox_SelectedIndexChanged(object sender, EventArgs e)
{
ValidateSelectedPackage();
}
void ValidateSelectedPackage()
{
if (packageListBox.SelectedIndex < 0)
return;
Cursor = Cursors.WaitCursor;
try
{
FileListItem fileListItem = (FileListItem)packageListBox.SelectedItem;
string filePath = fileListItem.FilePath;
logTextBox.Clear();
logTextBox.AppendText(String.Format("Validating: {0}\r\n\r\n", filePath));
using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
bool anyMessages = false;
using (PackageReader packageReader = PackageReader.Create(stream))
{
PackageValidatorSettings settings;
if (additionalScormWarningsCheckBox.Checked)
{
settings = new PackageValidatorSettings(
ValidationBehavior.LogWarning, ValidationBehavior.LogWarning,
ValidationBehavior.LogError, ValidationBehavior.LogWarning);
}
else
{
settings = new PackageValidatorSettings(
ValidationBehavior.LogWarning, ValidationBehavior.None,
ValidationBehavior.LogError, ValidationBehavior.LogWarning);
}
ValidationResults results;
try
{
results = PackageValidator.Validate(packageReader, settings);
}
catch (InvalidPackageException ex)
{
logTextBox.AppendText(String.Format("Package is invalid.\r\n\r\n{0}", ex.Message));
return;
}
foreach (ValidationResult result in results.Results)
{
string message;
if (result.IsError)
message = String.Format("MLC Error: {0}\r\n\r\n", result.Message);
else
message = String.Format("SCORM Warning: {0}\r\n\r\n", result.Message);
logTextBox.AppendText(message);
anyMessages = true;
}
}
if (!anyMessages)
logTextBox.AppendText("Package is valid.");
}
}
finally
{
Cursor = Cursors.Default;
}
}
private void additionalScormWarningsCheckBox_CheckedChanged(object sender, EventArgs e)
{
ValidateSelectedPackage();
}
}
class FileListItem
{
public string FilePath;
public FileListItem(string filePath)
{
FilePath = filePath;
}
public override string ToString()
{
return Path.GetFileName(FilePath);
}
}
}